package com.pzl.hwr;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

/**
 * 灵云开发平台配置
 */
public class HciCloudSetting {

    private static HciCloudSetting mInstance;

    private Map<String, String> mSettingMap;

    private HciCloudSetting() {
        mSettingMap = new HashMap<String, String>();
    }

    public static HciCloudSetting getInstance() {
        if (mInstance == null) {
            mInstance = new HciCloudSetting();
        }
        return mInstance;
    }

    public String getCapKey() {
        return mSettingMap.get("capKey");
    }

    public String getDeveloperKey() {
        return mSettingMap.get("developerKey");
    }

    public String getAppKey() {
        return mSettingMap.get("appKey");
    }

    public String getCloudUrl() {
        return mSettingMap.get("cloudUrl");
    }

    /**
     * 加载用户的注册信息
     *
     * @param
     */
    public boolean loadSetting(Context context) {
        boolean isSuccess = true;
        try {
            InputStream in = null;
            in = context.getResources().getAssets().open("HciCloudSetting.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(in,
                    "utf-8");
            BufferedReader br = new BufferedReader(inputStreamReader);
            String temp = null;
            String[] sInfo = new String[2];
            temp = br.readLine();
            while (temp != null) {
                if (!temp.startsWith("#") && !temp.equalsIgnoreCase("")) {
                    sInfo = temp.split("=");
                    if (sInfo.length == 2) {
                        if (sInfo[1] == null || sInfo[1].length() <= 0) {
                            isSuccess = false;
                            Log.e("HciCloudSetting", sInfo[0] + "is null");
                            break;
                        }
                        mSettingMap.put(sInfo[0], sInfo[1]);
                    }
                }
                temp = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            isSuccess = false;
        }

        return isSuccess;
    }


}
